<?php

namespace app\common\model\mycurrency;

use think\Exception;
use think\Model;
use traits\model\SoftDelete;

class OperateCoupons extends Model
{

    //use SoftDelete;

    

    // 表名
    protected $name = 'mycurrency_operate_coupons';

    // 追加属性
    protected $append = [
        'discount_type_text',
        'use_expire_type_text',
        'status_text',
    ];

    const DISCOUNT_TYPE_ZHEKOU          = 1;//优惠券类型 = 折扣
    const DISCOUNT_TYPE_MANJIAN         = 2;//优惠券类型 = 满减
    const DISCOUNT_TYPE_LIJIAN          = 3;//优惠券类型 = 立减

    const USE_EXPIRE_TYPE_SHICHANG      = 1;//使用到期类型 = 领取后N天可用
    const USE_EXPIRE_TYPE_SHIJIANDUAN   = 2;//使用到期类型 = 指定时间段可用

    const STATUS_ZHENGCHENG             = 1;//状态 = 正常
    const STATUS_JINGYONG               = 2;//状态 = 禁用


    /**
     * 优惠券添加 数据合法性验证
    */
    public static function addDataCheck($params){
        if($params['discount_type'] == self::DISCOUNT_TYPE_ZHEKOU){//优惠类型:1=折扣
            if($params['discount_ratio'] == null){
                throw new Exception('折扣比例不能为空');
                return false;
            }
            if($params['discount_ratio'] <= 0 || $params['discount_ratio'] >= 10){
                throw new Exception('折扣比例必须是大于0小于10的数字');
                return false;
            }
        }elseif ($params['discount_type'] == self::DISCOUNT_TYPE_MANJIAN){//优惠类型:2=满减
            if($params['use_consumption_limit'] == null){
                throw new Exception('消费限制金额不能为空');
                return false;
            }
            if($params['full_reduction_fee'] == null){
                throw new Exception('满减金额不能为空');
                return false;
            }
            if($params['full_reduction_fee'] > $params['use_consumption_limit']){
                throw new Exception('满减金额不能大于等于消费限制金额');
                return false;
            }
        }elseif ($params['discount_type'] == self::DISCOUNT_TYPE_MANJIAN){//优惠类型:3=立减
            if($params['lijian_fee'] == null){
                throw new Exception('立减金额不能为空');
                return false;
            }
        }
        if ($params['receive_start_time'] != null && $params['receive_end_time'] != null ){
            if(strtotime($params['receive_start_time']) >= strtotime($params['receive_end_time'])){
                throw new Exception('领取开始时间不能大于领取结束时间');
                return false;
            }
        }
        if($params['use_expire_type'] == self::USE_EXPIRE_TYPE_SHICHANG){//使用到期类型:1=领取后N天可用
            if($params['effective_day'] == null || $params['effective_day'] < 0){
                throw new Exception('领取后有效期天数必须是大于0的整数');
                return false;
            }
        }elseif ($params['use_expire_type'] == self::USE_EXPIRE_TYPE_SHIJIANDUAN){//使用到期类型:2=指定时间段可用
            if($params['use_start_time'] == null || $params['use_end_time'] == null){
                throw new Exception('使用有效期时间段不能为空');
                return false;
            }
            if(strtotime($params['use_start_time']) >= strtotime($params['use_end_time'])){
                throw new Exception('使用开始时间不能大于使用结束时间');
                return false;
            }
        }
        if($params['receive_number'] <= 0){
            throw new Exception('没人限领张数不能小于0');
            return false;
        }
        if($params['tatal'] <= 0){
            throw new Exception('优惠券发放张数不能小于0');
            return false;
        }
    }


    public function getReceiveStartTimeAttr($value){
        return $value != null ? date('Y-m-d', $value) : '';
    }

    public function getReceiveEndTimeAttr($value){
        return $value != null ? date('Y-m-d', $value) : '';
    }

    public function getUseStartTimeAttr($value){
        return $value != null ? date('Y-m-d', $value) : '';
    }

    public function getUseEndTimeAttr($value){
        return $value != null ? date('Y-m-d', $value) : '';
    }

    public function getDiscountTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['discount_type']) ? $data['discount_type'] : '');
        $list = ['1' => __('折扣'), '2' => __('满减'), '3' => __('立减')];;
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getUseExpireTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['use_expire_type']) ? $data['use_expire_type'] : '');
        $list = ['1' => __('领取后N天可用'), '2' => __('指定时间段可用')];;
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = ['1' => __('正常'), '2' => __('禁用')];;
        return isset($list[$value]) ? $list[$value] : '';
    }


}
